layui.use('layer', function () {
    let $ = layui.$
    let layer = layui.layer

    let countdown = 60;
    function countDown(obj) {
        if (countdown == 0) {
            obj.removeAttribute("disabled");
            obj.value = "获取验证码";
            countdown = 60;
            return;
        } else {
            obj.setAttribute("disabled", true);
            obj.value = "重新发送(" + countdown + ")";
            countdown--;
        }
        setTimeout(function () {
            countDown(obj)
        }
            , 1000)
    }

    // validate Phone number
    let vcode;
    document.getElementById('btn-getVcode').addEventListener('click', function () {
        let inputPhone = document.getElementById('input_phone').value;
        if (inputPhone == '') {
            layer.msg('手机号不能为空');
            return false;
        } else if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(inputPhone))) {
            layer.msg("手机号不正确");
            return false;
        } else if (inputPhone.length != 11) {
            layer.msg("手机号不正确");
            return false;
        } else {
            countDown(this);
            vcode = getCode(inputPhone);
        }
    });

    // Handle register button on click
    document.getElementById('register').addEventListener('tap', function () {
        let inputPhone = document.getElementById('input_phone').value;
        let inputVcode = document.getElementById('input_vcode').value;
        let inputPwd = document.getElementById('input_pwd').value;
        if (inputPhone == '') {
            layer.msg('手机号不能为空');
            return false;
        }
        else if (inputPhone.length != 11) {
            layer.msg('手机号不正确');
            return false;
        } else if (inputVcode == '') {
            layer.msg('请填写验证码');
            return false;
        } else if (inputPwd == '' || inputPwd.length < 6) {
            layer.msg('密码不能为空并且不能少于6个字符')
        } else {
            if (inputVcode == vcode) {
                console.log("注册成功");
                addUser(inputPhone, inputPwd);
                layer.msg('注册成功！！')
                out(function () {
                    mui.back();
                }
                    , 1000)
            } else {
                layer.msg('验证码输入不对')
            }
        };
    });

    // Request server to add user
    function addUser(phone, pwd) {
        $.ajax({
            type: 'post',
            contentType: "application/json",
            url: 'https://myserver.com/register.php',
            dataType: "json",
            data: {//data携带的参数，根据自己的服务器参数写就ok；
                phone: phone,
                pwd: pwd,
            },
            success: function (data) {
                console.log(JSON.stringify(data.d));
            }
        });
    }

    /**
     *  get code from server
     * @param {string} tels 
     */
    function getCode(tels) {
        let t_vcode;
        let data = {
            phone: tels,
            msg: '',
        }
        $.ajax({
            type: 'post',
            contentType: "application/json",
            url: 'https://myserver.com/getVcode.php',
            dataType: "json",
            async: false,
            data: JSON.stringify(data),
            success: function (data) {//成功的data函数
                let json = eval('(' + data.d + ")");
                t_vcode = json.code;
                console.log("返回的验证：" + t_vcode);
            }
        });
        return t_vcode;
    }
})
